<?php

namespace app\data\controller\shop;

use app\data\model\ShopGoods;
use app\data\model\ShopGoodsBrand;
use app\data\model\ShopGoodsBrandRela;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;

/**
 * 商品品牌管理
 * Class Cate
 * @package app\data\controller\shop
 */
class Brand extends Controller
{
    /**
     * 商品品牌管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        ShopGoodsBrand::mQuery()->layTable(function () {
            $this->title = "商品品牌管理";
        }, function (QueryHelper $query) {
            $query->whereIn('status','0,1');
            $query->like('name')->equal('status')->dateBetween('create_at');
        });
    }

    /**
     * 添加商品品牌
     * @auth true
     */
    public function add()
    {
        $this->addtype = 1;
        ShopGoodsBrand::mForm('form');
    }

    /**
     * 编辑商品品牌
     * @auth true
     */
    public function edit()
    {
        $this->addtype = 0;
        ShopGoodsBrand::mForm('form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {

        }else{
            $data['create_at'] = \Tools::getDateTimeByTime();
        }
    }

    /**
     * 修改商品品牌状态
     * @auth true
     */
    public function state()
    {
        ShopGoodsBrand::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除商品品牌
     * @auth true
     */
    public function remove()
    {
        ShopGoodsBrand::mDelete();
    }

    /**
     * 查看品牌商品
     * @auth true
     */
    public function brandgoods(){
        $id = input('id','');
        if($this->request->isPost()){
            $goods_id = input('goods_id','');
            $res = ShopGoodsBrandRela::mk()->where(array(
                'brand_id'  => $id,
                'goods_id'  => $goods_id,
            ))->save(array(
                'sort'  => input('sort')
            ));
            if($res){
                return \Tools::returnMsg(true,$res,'json');
            }else{
                return \Tools::returnMsg(false,array(
                    'code'  => '10004',
                    'message'=>'失败'
                ),'json');
            }
        }else {
            $from = input('from',0);
            $this->title = '品牌商品管理';
            $this->brand_id = $id;
            $query = ShopGoods::mQuery();
            if($from == 0){
                $goods_ids = ShopGoodsBrandRela::mk()->where(['brand_id' => $id])->column('goods_id');
                $query->like('code|name#name');
                $lists = $query->whereIn('id', $goods_ids)->equal('status')->order(array(
                    'sort' => 'desc',
                    'create_at' => 'desc'
                ))->page(true, false, false);
            }else{
                $query->like('code|name#name');
                $lists = $query->equal('status')->order(array(
                    'sort' => 'desc',
                    'create_at' => 'desc'
                ))->page(true, false, false);
            }

            if (empty($lists['list']) === FALSE) {
                foreach ($lists['list'] as &$lv) {
                    $sortinfo = ShopGoodsBrandRela::mk()->where(['brand_id' => $id, 'goods_id' => $lv['id']])->value('sort');
                    $lv['sort'] = $sortinfo;
                }
            }
            $this->list = $lists['list'];
            $this->fetch();
        }
    }
    /**
     * 添加品牌商品
     * @auth true
     */
    public function bgadd(){
        $brand_id = input('brand_id');
        $this->title = '添加品牌商品';
        $this->brand_id = $brand_id;
        if($this->request->isPost()){
            $goods_id = input('goods_id','');
            $goods_id = explode(',',$goods_id);
            foreach($goods_id as &$gv){
                $info = ShopGoodsBrandRela::mk()->where(array(
                    'brand_id'  => $brand_id,
                    'goods_id'  => $gv,
                ))->find();
                if(empty($info)) {
                    $sort = ShopGoodsBrandRela::mk()->where(array(
                        'brand_id'  => $brand_id
                    ))->order(array(
                        'sort'  => 'desc'
                    ))->value('sort');
                    if(empty($sort)) $sort = 0;
                    $res = ShopGoodsBrandRela::mk()->insert(array(
                        'brand_id' => $brand_id,
                        'goods_id' => $gv,
                        'create_at' => \Tools::getDateTimeByTime(),
                        'sort' => $sort+1
                    ));
                }
            }
            return \Tools::returnMsg(true,[],'json');
        }else {
            $query = ShopGoods::mQuery();
            $query->like('code|name#name');
            $lists = $query->equal('status')->order(array(
                'sort' => 'desc',
                'create_at' => 'desc'
            ))->page(true, false, false);
            if (empty($lists['list']) === FALSE) {
                foreach ($lists['list'] as &$lv) {
                    $lv['selected'] = 0;
                    $sortinfo = ShopGoodsBrandRela::mk()->where(['brand_id' => $brand_id, 'goods_id' => $lv['id']])->find();
                    if(empty($sortinfo) === FALSE){
                        $lv['selected'] = 1;
                    }
                }
            }
            $this->list = $lists['list'];
            $this->fetch();
        }
    }
    /**
     * 移出品牌商品
     * @auth true
     */
    public function bgremove(){
        $goods_id = input('goods_id','','trim');
        $brand_id = input('brand_id','','trim,intval');
        $res = ShopGoodsBrandRela::mk()->where(array(
            'brand_id'  => $brand_id
        ))->whereIn('goods_id',$goods_id)->delete();
        if($res){
            return \Tools::returnMsg(true,$res,'json');
        }else{
            return \Tools::returnMsg(false,array(
                'code'  => '10004',
                'message'=>'移出失败'
            ),'json');
        }
    }
}
